home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / util / libs / graphics3d.lha / src / library / graphics3dl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-16  |  5.0 KB  |  224 lines

  1. /*
  2. **      $VER: graphics3Dl.c 10.00 (14.11.98)
  3. **
  4. **      Objects loader functions for graphics3D.library
  5. **
  6. **      (C) Copyright 97 Patrizio Biancalani
  7. **      All Rights Reserved.
  8. **
  9. */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <proto/exec.h>
  14. #include <dos/dos.h>
  15. #include <proto/intuition.h>
  16. #include <intuition/intuition.h>
  17. #include <intuition/screens.h>
  18.  
  19. #include <graphics/rastport.h>
  20. #include <graphics/clip.h>
  21. #include <graphics/regions.h>
  22. #include <graphics/gfx.h>
  23. #include <graphics/gfxmacros.h>
  24. #include <graphics/layers.h>
  25.  
  26. #include "graphics3Dc.h"
  27. #include "graphics3D.h"
  28. #include "graphics3Df.h"
  29. #include "graphics3df_proto.h"
  30. /******************************************************************
  31.  * Explanation of my custom 3d object format "3dgfo" :
  32.  ** Header :
  33.  * [3][D][G][F][X][V][x][.][x][x][00][object_name][00]
  34.  * total vertex number (4 bytes :long int)
  35.  * total polys number (4 bytes :long int)
  36.  ** vertex coordinates :
  37.  * X coordinates (8 bytes :double float Ieee format)
  38.  * Y coordinates (8 bytes :double float Ieee format)
  39.  * Z coordinates (8 bytes :double float Ieee format)
  40.  * Note : One triplet for any vertex .
  41.  ** poligon descriptor :
  42.  * flag+colour of polygon (4 bytes)
  43.  * vertex number of polygon (1 byte :permit value 1,2,3,4)
  44.  * #1 vertex index (4 bytes :long int ,first =0 last=vertex_number-1)
  45.  * #2 vertex index (4 bytes :long int ,first =0 last=vertex_number-1)
  46.  * #3 vertex index (4 bytes :long int ,first =0 last=vertex_number-1)
  47.  * #4 vertex index (4 bytes :long int ,first =0 last=vertex_number-1)
  48.  * Note : vertex index #2,#3,#4 is present only if vertex number of polygon
  49.  *      is respectly 2,3 or 4.
  50.  * Description of flag+colour :
  51.  * flag (1 byte)
  52.  * bit 0 = 0 
  53.  *        colour[0] is Red component     (1 byte: 0-255)
  54.  *        colour[1] is Green component    (1 byte: 0-255)
  55.  *        colour[3] is Blue component    (1 byte: 0-255)
  56.  * bit 0 = 1
  57.  *        colour[0] ignored
  58.  *        colour[1] ignored
  59.  *        colour[3] register colour on screen palette (1 byte: 0-255)
  60.  *      Note : now only bit0=1 is supported
  61.  * bit 1 = 1 two face polygon
  62.  * bit 1 = 0 one face polygon
  63.  * bit 2-7 = reserved for future use
  64.  **********************************************************************/
  65.  
  66. /*** local define ***/
  67. #define HEADER_LEN 11
  68.  
  69. /*** local prototipe ***/
  70. void lo_end(struct ambient3d *in,long int id,long int f);
  71.  
  72.  
  73. /************************************************
  74.  ** Carica un oggetto nel formato custom .3dgfo**
  75.  ** nella scena 3d.                   **
  76.  ************************************************
  77.  *** INPUT :                        * 
  78.  * in    -> valore > 0 restituito da display3d.    *
  79.  * foname-> puntatore a stringa con nome file.  *
  80.  * scale -> fattore scala oggetto in fix point  * 
  81.  *** OUTPUT:                    *
  82.  * > 0 tutto ok, rende n# identificatore oggetto*
  83.  * =0 errore, operazione fallita.        * 
  84.  ************************************************/
  85. long int GD_loadobject(in,foname,scale)
  86. REG(a0)struct ambient3d *in;
  87. REG(a1)char *foname;
  88. REG(d0)long int scale;
  89. {
  90. double x,y,z;
  91. long int sc,i,f,cl,esi,e,id;
  92. long int np,n_vert,n_pol;
  93. long int x1,y1,z1;
  94. long int fl,fc,p1,p2,p3,p4;
  95. char name[80],c;
  96.  
  97. #ifdef DEBUG
  98. char dbg[80];
  99. #endif
  100.  
  101. sc=scale;
  102. esi=0;
  103. id=0;
  104.  
  105. f=Open(foname,MODE_OLDFILE);
  106. if (f==NULL) return(esi);
  107.  
  108. /* read object identifier */
  109. e=Read(f,name,HEADER_LEN);
  110. if (e!=HEADER_LEN) goto abort;
  111.  
  112. if (name[0]!='3' OR name[1]!='D' OR name[2]!='G' OR name[3]!='F' OR
  113.     name[4]!='X' OR name[5]!='V' ) goto abort;
  114.  
  115. /* read header */
  116.  
  117. #ifdef DEBUG
  118. sprintf(dbg,"header=%s\n",name);
  119. write_dbg(dbg);
  120. #endif
  121.  
  122. i=0;
  123. e=Read(f,&c,1);
  124. while (e==1 AND c!=0x00)
  125.     {
  126.     if (i<78) name[i++]=c;
  127.     e=Read(f,&c,1);
  128.     }
  129. if (e!=1) goto abort;
  130. name[i]=0x00;
  131.  
  132. e=Read(f,&n_vert,4);
  133. if (e!=4) goto abort;
  134. e=Read(f,&n_pol,4);
  135. if (e!=4) goto abort;
  136.  
  137. if (n_vert==NULL OR n_pol==NULL) goto abort;
  138.  
  139. e=GD_newobj(in,name,n_pol,n_vert);
  140. if (e==0) goto abort;
  141. id=e;
  142.  
  143. /* read all vertex */
  144. for (i=0 ; i<n_vert ; i++)
  145.     {
  146.     x1=0;
  147.     y1=0;
  148.     z1=0;
  149.     e=Read(f,&x,8);
  150.     if (e!=8) goto abort;
  151.     GD_dfl2fix(&x,&x1);
  152.     x1=(x1*sc)>>SFIXV;
  153.     e=Read(f,&y,8);
  154.     if (e!=8) goto abort;
  155.     GD_dfl2fix(&y,&y1);
  156.     y1=(y1*sc)>>SFIXV;
  157.     e=Read(f,&z,8);
  158.     if (e!=8) goto abort;
  159.     GD_dfl2fix(&z,&z1);
  160.     z1=(z1*sc)>>SFIXV;
  161.     GD_addobjvertex(in,i,x1,y1,z1);
  162.     }
  163.  
  164. /* read all polygon descriptor */
  165. for (i=0 ; i<n_pol ;i++)
  166.     {
  167.     e=Read(f,&fc,4);
  168.     if (e!=4) goto abort;
  169.     e=Read(f,&c,1);
  170.     if (e!=1) goto abort;
  171.     e=Read(f,&p1,4);
  172.     if (e!=4) goto abort;
  173.     p2=-1;
  174.     p3=-1;
  175.     p4=-1;
  176.     np=(long int)c;
  177.     switch(np)
  178.         {
  179.         case 2:
  180.             e=Read(f,&p2,4);
  181.             if (e!=4) goto abort;
  182.             break;
  183.         case 3:
  184.             e=Read(f,&p2,4);
  185.             if (e!=4) goto abort;
  186.             e=Read(f,&p3,4);
  187.             if (e!=4) goto abort;
  188.             break;
  189.         default:    
  190.             e=Read(f,&p2,4);
  191.             if (e!=4) goto abort;
  192.             e=Read(f,&p3,4);
  193.             if (e!=4) goto abort;
  194.             e=Read(f,&p4,4);
  195.             if (e!=4) goto abort;
  196.         }    
  197.     GD_addobjpoly(in,i,p1,p2,p3,p4);
  198.     cl=0x000000FF & fc;
  199.     fl=0x02000000 & fc;
  200.     if (fl!=0) fl=1;
  201.     GD_cattpoly(in,i,cl,fl);
  202.     }
  203.  
  204. GD_recalcobj(in);
  205. esi=id;
  206. id=0;
  207.  
  208. abort:
  209. lo_end(in,id,f);
  210.  
  211. return(esi);
  212. }
  213.  
  214. void lo_end(in,id,f)
  215. struct ambient3d *in;
  216. long int id;
  217. long int f;
  218. {
  219.  
  220. if (id!=NULL) GD_deleteobject(in);
  221. Close(f); 
  222.  
  223. }
  224.